package com.bingxue.edu.parent.service;


import com.bingxue.edu.management.education.service.exam.ExamRecordService;
import com.bingxue.edu.parent.model.resp.ExamQuestionDistResp;
import com.bingxue.edu.parent.model.resp.ExamScoreDistResp;
import com.bingxue.edu.parent.model.resp.ParentMyScoreDetailResp;
import com.bingxue.edu.parent.model.resp.ParentMyScoreResp;
import com.bingxue.edu.parent.model.resp.StudentExamRecordReportResp;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.bingxue.edu.management.education.model.entity.exam.table.ExamRecordTableDef.EXAM_RECORD;
import static com.bingxue.edu.management.education.model.entity.exam.table.ExamTableDef.EXAM;
import static com.bingxue.edu.management.education.model.entity.table.ClassInfoTableDef.CLASS_INFO;

@Slf4j
@Service
@RequiredArgsConstructor
public class StudentExamService {
    private final ExamRecordService examRecordService;

    /**
     * 根据学生id获取学生的考试成绩列表
     * @param studentId 学生id
     */
    public List<ParentMyScoreResp> getMyScore(String studentId) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(EXAM_RECORD.ID.as(ParentMyScoreResp::getExamRecordId),
                        EXAM_RECORD.EXAM_ID.as(ParentMyScoreResp::getExamId),
                        EXAM_RECORD.STUDENT_ID.as(ParentMyScoreResp::getStudentId),
                        CLASS_INFO.NAME.as(ParentMyScoreResp::getClassName),
                        EXAM_RECORD.CLASS_ID.as(ParentMyScoreResp::getClassId),
                        EXAM_RECORD.TOTAL_SCORE.as(ParentMyScoreResp::getTotalScore),
                        EXAM_RECORD.RANK.as(ParentMyScoreResp::getRank),
                        EXAM.NAME.as(ParentMyScoreResp::getExamName),
                        EXAM.EXAM_TYPE.as(ParentMyScoreResp::getExamType))
                .from(EXAM_RECORD)
                .leftJoin(EXAM).on(EXAM_RECORD.EXAM_ID.eq(EXAM.ID))
                .leftJoin(CLASS_INFO).on(EXAM_RECORD.CLASS_ID.eq(CLASS_INFO.ID))
                .where(EXAM_RECORD.STUDENT_ID.eq(studentId))
                .orderBy(EXAM_RECORD.TOTAL_SCORE.desc());

        return examRecordService.listAs(queryWrapper, ParentMyScoreResp.class);
    }

    /**
     * 根据考试记录id获取学生的考试成绩详情
     * @param examRecordId 考试记录id
     */
    public ParentMyScoreDetailResp getMyScoreDetail(String examRecordId) {

        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(EXAM_RECORD.ID.as(ParentMyScoreDetailResp::getExamRecordId),
                        EXAM_RECORD.EXAM_ID.as(ParentMyScoreDetailResp::getExamId),
                        EXAM_RECORD.STUDENT_ID.as(ParentMyScoreDetailResp::getStudentId),
                        CLASS_INFO.NAME.as(ParentMyScoreDetailResp::getClassName),
                        EXAM_RECORD.CLASS_ID.as(ParentMyScoreDetailResp::getClassId),
                        EXAM_RECORD.TOTAL_SCORE.as(ParentMyScoreDetailResp::getTotalScore),
                        EXAM_RECORD.RANK.as(ParentMyScoreDetailResp::getRank))
                .from(EXAM_RECORD)
                .leftJoin(CLASS_INFO).on(EXAM_RECORD.CLASS_ID.eq(CLASS_INFO.ID))
                .where(EXAM_RECORD.ID.eq(examRecordId));

        return examRecordService.getMapper().selectOneWithRelationsByQueryAs(queryWrapper, ParentMyScoreDetailResp.class);
    }

    public StudentExamRecordReportResp getRecordReport(String examId, String examRecordId) {

        ExamScoreDistResp scoreDist = examRecordService.getScoreDist(examId);
        ExamQuestionDistResp questionDist = examRecordService.getStudentScoreDist(examId, examRecordId);

        StudentExamRecordReportResp resp = new StudentExamRecordReportResp();
        resp.setQuestionDist(questionDist);
        resp.setScoreDist(scoreDist);
        return null;
    }
}
